package su.mvc.action.image;

import java.util.logging.Logger;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import su.mvc.action.ActionSupport;
import su.mvc.model.Image;
import su.mvc.service.gui.ImageService;


@Results(value = {
        @Result(name = ActionSupport.IMAGE, type = ActionSupport.IMAGE)
})
@ParentPackage("imagePack")
public class ImageAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(ImageAction.class.getName());

    private ImageService imageService;


    private byte[] imageInByte = null;
    private Long imageId;
    private Image image;
    private String contentType;

    private boolean icon = false;
    
    public String execute() throws Exception{
        image = imageService.getImageById(getImageId());


        if (!isIcon()) {
            setImageInByte(image.getImage());
        } else {
            setImageInByte(image.getIcon());
        }
        setContentType(image.getContentType());
        return IMAGE;
    }


    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public byte[] getImageInByte() {
        return imageInByte;
    }

    public void setImageInByte(byte[] imageInByte) {
        this.imageInByte = imageInByte;
    }

    public Long getImageId() {
        return imageId;
    }

    public void setImageId(Long imageId) {
        this.imageId = imageId;
    }

    public ImageService getImageService() {
        return imageService;
    }

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public boolean isIcon() {
        return icon;
    }

    public void setIcon(boolean icon) {
        this.icon = icon;
    }
}
